package com.wqr.it.ecwdr.client.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wqr.it.ecwdr.client.dto.ProductDto;
import com.wqr.it.ecwdr.client.dto.StockDto;
import com.wqr.it.ecwdr.client.souq.reader.ProductSalesInfoListReader;
import com.wqr.it.ecwdr.client.util.DateUtils;
import com.wqr.it.ecwdr.client.util.WebClientUtil;

//@Async
@Service
public class StockService {
	private static final Logger log = LoggerFactory.getLogger("INFO");
	@Value("${ecwdr.server.url.stock.product}")
	private String stockProductUrl;
	@Value("${ecwdr.server.url.stock.post}")
	private String stockPostUrl;
	//private static String charset = "UTF-8";
	@Autowired
	private String ecwdrClientName;
	
	private int stockProductQueueSize = 100;
	
	private ArrayBlockingQueue<ProductDto> productQueue = new ArrayBlockingQueue<>(stockProductQueueSize);
	
	//@Autowired
	//private Map<String, CountryDto> countryMap;

	@Autowired
	private ProductSalesInfoListReader productSalesInfoListReader;
	
	public StockService() {
		
	}
	
	@Scheduled(fixedDelay=50000) 
	public void schedule() {
		//System.out.println(stockProductUrl);
		//WebClientUtil.doGet(stockProductUrl, null);
        String json = WebClientUtil.doGet(stockProductUrl, null);
        System.out.println(DateUtils.currentDate() + " StockService.schedule " + json);
        if (!StringUtils.isEmpty(json)) {
        	List<ProductDto> array = JSONArray.parseArray(json, ProductDto.class);
        	for (ProductDto productDto : array) {
        		try {
					productQueue.put(productDto);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
        	}
        }
	}

	@Scheduled(fixedRate=300) 
	public void stock() {
        System.out.println(DateUtils.currentDate() + " StockService.stock ");
		ProductDto productDto = productQueue.poll();
        System.out.println(DateUtils.currentDate() + " StockService.stock " + productDto);
		if (productDto != null) {
			try {
				StockDto stockDto = productSalesInfoListReader.getProductSalesInfoList(productDto.getNumber(), productDto.getCountryId());
				if (stockDto != null && stockDto.getItemList() != null) {
					stockDto.setCountryId(productDto.getCountryId());
					stockDto.setSourceId(productDto.getSourceId());
					stockDto.setClientName(ecwdrClientName);
					//System.out.println(stockDto);
					Map<String, String> params = new HashMap<>();
					params.put("StockDto", JSONObject.toJSONString(stockDto));
					System.out.println(params.get("StockDto"));
					try {
						WebClientUtil.doPost(stockPostUrl, params );
					} catch(Exception e) {
						try {
							Thread.sleep(20000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
						WebClientUtil.doPost(stockPostUrl, params );
					}
				}
			} catch (Exception e) {
				log.error(productDto + " <== " + e.getMessage());
				e.printStackTrace();
			}
		} else {
			schedule();
		}
	}	
}
